/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FitnessInfo.java
 *
 * Created on Aug 10, 2011, 9:40:57 AM
 */
package GUI;

import Admin.InitData;
import Class.Fitness;
import java.awt.Color;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Tomcat
 */
public class FitnessInfoAdmin extends javax.swing.JPanel {

    JFrame frame;
    Vector data;
    ResultSet rs;
    Statement st;
    Connection cn;

    /** Creates new form FitnessInfo */
    public FitnessInfoAdmin(javax.swing.JFrame frame) {
        this.frame = frame;
        initComponents();
        data = InitData.InitFitnessTableData(tblFitness);
        int widths[] = {10, 50, 10, 10, 10, 150, 50, 10};
        for (int i = 0; i < 8; i++) {
            TableColumn column = tblFitness.getColumnModel().getColumn(i);
            column.setPreferredWidth(widths[i]);
        }
        tblFitness.setRowMargin(3);
        tblFitness.setAutoscrolls(true);
        tblFitness.setGridColor(Color.MAGENTA);
        tblFitness.setSelectionBackground(Color.BLUE);
        tblFitness.setRowHeight(tblFitness.getRowHeight() + 5);
        tblFitness.setShowHorizontalLines(false);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblFitness = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        txtKeyword = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        lbResult = new javax.swing.JLabel();
        btnAdvSearch = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 204));
        setPreferredSize(new java.awt.Dimension(726, 400));

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Fitness List"));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 204));

        tblFitness.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblFitness.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblFitnessMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblFitness);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Fitness"));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/edit_add.png"))); // NOI18N
        btnAdd.setText("Add New");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add user.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/button_cancel.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/logout.png"))); // NOI18N
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(btnAdd)
                .addGap(42, 42, 42)
                .addComponent(btnUpdate)
                .addGap(46, 46, 46)
                .addComponent(btnDelete)
                .addGap(52, 52, 52)
                .addComponent(btnRefresh)
                .addGap(97, 97, 97))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnRefresh))
                .addGap(50, 50, 50))
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 204));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Search"));

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel16.setForeground(new java.awt.Color(102, 0, 102));
        jLabel16.setText("Fitness Name");

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 10));
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search_48.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        lbResult.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbResult.setForeground(new java.awt.Color(255, 0, 0));

        btnAdvSearch.setFont(new java.awt.Font("Tahoma", 1, 10));
        btnAdvSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search(2).png"))); // NOI18N
        btnAdvSearch.setText("Advance Search");
        btnAdvSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdvSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel16)
                        .addGap(18, 18, 18)
                        .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(btnSearch)
                        .addGap(18, 18, 18)
                        .addComponent(btnAdvSearch))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(lbResult, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                        .addGap(403, 403, 403)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdvSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbResult, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblFitnessMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFitnessMouseClicked
}//GEN-LAST:event_tblFitnessMouseClicked

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        FitnessAddNew addFitness = new FitnessAddNew(frame, true);
        addFitness.setVisible(true);
}//GEN-LAST:event_btnAddActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        data = InitData.InitFitnessTableData(tblFitness);
        txtKeyword.setText("");
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        int[] selectedRows = tblFitness.getSelectedRows();
        if (selectedRows.length > 1) {
            JOptionPane.showMessageDialog(this, "Cannot update more than one in same time", "ERROR UPDATE", JOptionPane.ERROR_MESSAGE);
        } else if (selectedRows.length == 0) {
            JOptionPane.showMessageDialog(this, "Select at least one row", "ERROR", JOptionPane.OK_OPTION);
        } else {
            int updateNumber = selectedRows[0];
            Vector selectedVector = (Vector) data.get(updateNumber);
            String idUpdate = selectedVector.get(0).toString();
            FitnessUpdateAdmin fua = new FitnessUpdateAdmin(idUpdate, frame, true);
            fua.setResizable(false);
            fua.setVisible(true);
            fua.setLocationRelativeTo(null);
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int[] selectedRows = tblFitness.getSelectedRows();
        if (selectedRows.length == 0) {
            JOptionPane.showMessageDialog(this, "Select at least one row", "ERROR", JOptionPane.OK_OPTION);
        } else {
            int deleteAnswer = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (deleteAnswer != JOptionPane.YES_OPTION) {
                return;
            }
            int resultDelete = InitData.DeleteFitness(data, tblFitness);
            if (resultDelete > 0) {
                JOptionPane.showMessageDialog(this, "Delete Successfully!");
            } else {
                JOptionPane.showMessageDialog(this, "Delete Fail!");
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        Vector header = new Vector();
        Vector data = new Vector();
        String keyword = txtKeyword.getText();
        Fitness[] result = InitData.Search(keyword);
        if (result == null) {
            JOptionPane.showMessageDialog(this, "No result found", "SEARCH RESULT", JOptionPane.OK_OPTION);
            return;
        } else {
            header.add("Fitness ID");
            header.add("Fitness Name");
            header.add("Fee");
            header.add("Train Time");
            header.add("Hour/Day");
            header.add("Fitness Content");
            header.add("Fitness Image");
            header.add("Status");
            for (int i = 0; i < result.length; i++) {
                Vector row = new Vector();
                row.add(result[i].getFitID());
                row.add(result[i].getFitName());
                row.add(result[i].getFee());
                row.add(result[i].getTrainPeriod());
                row.add(result[i].getHour());
                row.add(result[i].getFitContent());
                row.add(result[i].getFitImage());
                row.add(result[i].getFitStatus());
                data.add(row);
            }
        }
        tblFitness.setModel(new DefaultTableModel(data, header));
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnAdvSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdvSearchActionPerformed
        FitnessAdvSearch fas = new FitnessAdvSearch(frame, true, tblFitness);
        fas.setVisible(true);
    }//GEN-LAST:event_btnAdvSearchActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAdvSearch;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbResult;
    private javax.swing.JTable tblFitness;
    private javax.swing.JTextField txtKeyword;
    // End of variables declaration//GEN-END:variables
}
